<div class="layuimini-main">

    <div class="layui-form layuimini-form">
		<input type="hidden" name="id" value="{{.menu.Id}}">
        <div class="layui-form-item">
			<label class="layui-form-label required">选择上级</label>
			<div class="layui-input-block">
			  <select name="parent_id" id = "parent_id" lay-filter="parent_id"  lay-verify="required">
				<option value="-1">-- 作为顶级菜单 --</option>
				{{range $index, $elem := .menuTreeList}}
					{{if compare  $elem.Id $.menu.ParentId}}
						<option value="{{$elem.Id}}" data-authority="{{$elem.Authority}}" selected>{{str2html $elem.PrefixStr}}{{$elem.AuthorityName}}</option>
					{{else}}
						<option value="{{$elem.Id}}" {{if compare  $elem.Id $.menu.Id}}disabled{{end}} data-authority="{{$elem.Authority}}" >{{str2html $elem.PrefixStr}}{{$elem.AuthorityName}}</option>
					{{end}}
					
				{{end}}
			  </select>
			</div>
		</div>
        
		
        <div class="layui-form-item">
            <label class="layui-form-label required">权限名</label>
            <div class="layui-input-block">
                <input type="text" name="authority_name" maxlength="30"   lay-verify="required" lay-reqtext="权限名不能为空" placeholder="请输入权限名" value="{{.menu.AuthorityName}}" class="layui-input">
              
            </div>
        </div>
		
        <div class="layui-form-item">
            <label class="layui-form-label required">状态</label>
            <div class="layui-input-block">
                <input type="radio" {{if compare  0 $.menu.Status}}checked{{end}} name="status" value="0" title="正常">
                <input type="radio" {{if compare  1 $.menu.Status}}checked{{end}} name="status" value="1" title="隐藏">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">菜单图标icon</label>
            <div class="layui-input-block">
                <input type="text" name="menu_icon" maxlength="30"  placeholder="请输入菜单icon 如： ‘layui-icon-set’" value="{{.menu.MenuIcon}}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单url</label>
            <div class="layui-input-block">
                <input type="text" name="menu_url" maxlength="255"  placeholder="请输入url" value="{{.menu.MenuUrl}}" class="layui-input">
            </div>
        </div>
		<div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" name="order_number" max="999999" onkeyup="if(value.length>6) value=value.substr(0,6)"  placeholder="请输入数字" value="{{.menu.OrderNumber}}" class="layui-input">
				 <tip>数字越大排序越靠前 范围：0 - 999999 </tip>
			</div>
        </div>
		
		<div class="layui-form-item">
            <label class="layui-form-label">权限标识</label>
            <div class="layui-input-block">
                <input type="text" name="authority" id="authority" maxlength="255"  placeholder="请输入权限标识" value="{{.menu.Authority}}" class="layui-input">
            </div>
        </div>
		
		<div class="layui-form-item">
            <label class="layui-form-label required">是否为 “按钮”</label>
            <div class="layui-input-block">
                <input type="radio" {{if compare  0 $.menu.IsMenu}}checked{{end}} name="is_menu" value="0" title="否" >
                <input type="radio" {{if compare  1 $.menu.IsMenu}}checked{{end}} name="is_menu" value="1" title="是">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'treetable' ,'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
			treetable = layui.treetable;
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        //var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
		
			var url = "/admin/rbac/menuEdit"
            $.post(url,data.field,function(resp){
				
				if(resp.code == 0){
					//成功
					layer.msg("编辑成功",{end:function(){
						//获取原始滚动条滚动的距离
						var layui_content = $(parent.document).find(".layuimini-content-page")
						var Tops = layui_content.scrollTop();
						console.log(Tops);
						//重新加载树形表格
						tree_table_render_params.url = "/admin/rbac/menu" //要刷新的数据源url
						treetable.reload(tree_table_render_params)
						//延迟调用：恢复滚动条滚动距离
						setTimeout("menus_edit_scroll_top("+Tops+")",800);
						
						 // 关闭弹出层
						layer.closeAll();
					}});
					
				}else{
					layer.msg(resp.msg);
				}
		    },"json");

            return false;
        });
		
		//上级菜单 与 权限标识联动 parent_id
		form.on('select(parent_id)',function (data){
			//console.log($(data.elem[data.elem.selectedIndex]).attr("data-authority"))
			var authority_val = $(data.elem[data.elem.selectedIndex]).attr("data-authority");
			var authority = ""
			if(authority_val){
				authority = authority_val+"."
			}
			$("#authority").val(authority);
		});
			
		//文字过滤
		$("#authority").keyup(function(){
			var biaoshi_str = $(this).val();
			var replace_str = biaoshi_str.replace(/[^a-zA-Z0-9\.]/g,'')
			$(this).val(replace_str);
			
		});

    });
	//菜单修改后滚动距离还原
	function menus_edit_scroll_top(Tops){
		//console.log(Tops);
		if(parseInt(Tops) > 0){
			$(parent.document).find(".layuimini-content-page").scrollTop(Tops);
			//console.log(Tops);
		}
	}
</script>